import axios from 'axios'

// 定义请求根路径与超时时限
const baseURL = '/api'
const timeout = 5000

const request = axios.create({
  baseURL,
  timeout,
})

// 请求拦截器
request.interceptors.request.use(config => {
    // 添加token到请求头中
    const token = sessionStorage.getItem('authorization')
    config.headers.Authorization = token
    return config
})

// 响应拦截器
request.interceptors.response.use(response => {
    if (response.status === 200 || response.status === 500) {
        return Promise.resolve(response.data)
    }
}, error => {
    // 响应失败
    return Promise.reject(error)
})

export default request